package com.dong.controller;

import com.dong.YOLOAPI.aiAPI.ImageInferenceService;
import com.dong.YOLOAPI.imagesrecognize1;
import com.dong.YOLOAPI.imagesrecognize2;
import com.dong.vedio.UdpVideoReceiverService;
import com.dong.vedio.vedio2.UdpVideo2ReceiverService;
import com.dong.vedio.vedio3.UdpVideo3ReceiverService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Base64;

@Slf4j
@RestController
@RequestMapping("getImages")
public class imagesController {
@Resource
imagesrecognize1 imagesrecognize1;
public  int time=1;
    @Autowired
    UdpVideoReceiverService udpVideoReceiverService;
    @Autowired
    UdpVideo2ReceiverService udpVideo2ReceiverService;
    @Autowired
    UdpVideo3ReceiverService udpVideo3ReceiverService;
@Resource
    ImageInferenceService imageInferenceService;
    @GetMapping(produces = MediaType.IMAGE_JPEG_VALUE) // 指定响应类型为 JPEG 图片（根据实际图片格式调整）
    public ResponseEntity<byte[]> getImage( String zoneid) {
        byte[] imageData=null;
        String choiced = "";
        HttpHeaders headers=null;
        if(zoneid.equals("one")) {
            choiced="one";
            // 1. 获取图片的二进制数据（假设你的服务中通过 frameData 字段存储）
            imageData = udpVideoReceiverService.frameData;
//            imagesrecognize1.broadcastFrame(imageData);

           
            // 2. 校验数据是否为空
            if (imageData == null || imageData.length == 0) {
                // 数据为空时返回 404 或 500 错误
                return new ResponseEntity<>(HttpStatus.NOT_FOUND);
            }
            log.info("识别接口调用次数{}😊",time++);
            // 3. 设置响应头（指定图片格式）
             headers = new HttpHeaders();
            headers.setContentType(MediaType.IMAGE_JPEG);
        } else if (zoneid.equals("two")) {
            choiced="two";
            // 1. 获取图片的二进制数据（假设你的服务中通过 frameData 字段存储）
            imageData = udpVideo2ReceiverService.frameData;
//            imagesrecognize2.broadcastFrame(imageData);
//            imagesrecognize1.broadcastFrame(imageData);
            // 2. 校验数据是否为空
            if (imageData == null || imageData.length == 0) {
                // 数据为空时返回 404 或 500 错误
                return new ResponseEntity<>(HttpStatus.NOT_FOUND);
            }
            log.info("识别接口调用次数{}😊",time++);
            // 3. 设置响应头（指定图片格式）
            headers = new HttpHeaders();
            headers.setContentType(MediaType.IMAGE_JPEG);

        } else if (zoneid.equals("three")) {
            choiced="three";
            // 1. 获取图片的二进制数据（假设你的服务中通过 frameData 字段存储）
            imageData = udpVideo3ReceiverService.frameData;
//            imagesrecognize1.broadcastFrame(imageData);
            // 2. 校验数据是否为空
            if (imageData == null || imageData.length == 0) {
                // 数据为空时返回 404 或 500 错误
                return new ResponseEntity<>(HttpStatus.NOT_FOUND);
            }
            log.info("识别接口调用次数{}😊{}",time++,choiced);
            // 3. 设置响应头（指定图片格式）
            headers = new HttpHeaders();
            // 根据实际图片格式设置 Content-Type：
            // - JPEG 用 MediaType.IMAGE_JPEG_VALUE
            // - PNG 用 MediaType.IMAGE_PNG_VALUE
            // - 其他格式（如 BMP）用 "image/bmp" 等
            headers.setContentType(MediaType.IMAGE_JPEG);

        }
        imageInferenceService.pullFromYOLOapiOne(Base64.getEncoder().encodeToString(imageData),choiced);


        // 4. 封装响应实体（二进制数据 + 头信息 + 200 状态码）
        return new ResponseEntity<>(imageData, headers, HttpStatus.OK);
    }
}
